<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="5">
					<a-form-item label="所属项目" name="projectCode">
						<ProjectSelect v-model:value="searchFormState.projectCode" />
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-form-item label="所属公司" name="deptId">
						<DeptSelect v-model:value="searchFormState.deptId" />
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-form-item label="回传时间">
						<a-range-picker v-model:value="dateRange" valueFormat="YYYY-MM-DD" :allowClear="false" />
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-space>
						<a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
						<a-button type="default" @click="reset">重置</a-button>
					</a-space>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false" class="mt-2">
		<s-table ref="tableRef" :columns="columns" :data="loadData" bordered :row-key="(record) => record.id">
			<template #operator>
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()">
						<template #icon>
							<plus-outlined />
						</template>
						新增
					</a-button>
				</a-space>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="tableRef.refresh(true)" />
	<Upload ref="uploadRef" @successful="tableRef.refresh(true)" />
</template>
<script setup>
	import Form from '@/views/bi/repayment/subsidiary/form.vue'
	import dayjs from 'dayjs'
	import { ref } from 'vue'
	import Upload from '@/views/bi/repayment/client/upload.vue'
	import subsidiaryRepaymentApi from '@/api/bi/repayment/subsidiary/subsidiaryRepaymentApi'
	import ProjectSelect from '@/components/Tjzy/Project/index.vue'
	import DeptSelect from '@/components/Tjzy/Dept/index.vue'

	const searchFormRef = ref()
	const searchFormState = ref({
		projectCode: undefined
	})
	const tableRef = ref()
	const formRef = ref()
	const uploadRef = ref()
	let dateRange = ref([
		dayjs().subtract(1, 'day').format('YYYY-MM-DD'),
		dayjs().format('YYYY-MM-DD') // 今天的日期
	])
	const columns = [
		{
			title: '项目名称',
			dataIndex: 'projectName'
		},
		{
			title: '批次',
			dataIndex: 'batch'
		},
		{
			title: '还款时间',
			dataIndex: 'repaymentDate'
		},
		{
			title: '还款金额',
			dataIndex: 'repaymentAmount'
		},
		{
			title: '分公司',
			dataIndex: 'deptName'
		}
	]
	const loadData = (parameter) => {
		searchFormState.value.current = parameter.current
		searchFormState.value.size = parameter.size
		searchFormState.value.beginTime = dateRange.value[0] + ' 00:00:00'
		searchFormState.value.endTime = dateRange.value[1] + ' 23:59:59'
		return subsidiaryRepaymentApi.pageList(searchFormState.value).then((res) => {
			return res
		})
	}

	const reset = () => {
		searchFormRef.value.resetFields()
		dateRange = ref([
			dayjs().subtract(1, 'day').format('YYYY-MM-DD'),
			dayjs().format('YYYY-MM-DD') // 今天的日期
		])
		tableRef.value.refresh(true)
	}
</script>
